package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import io.swagger.annotations.Api;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：MJS
 * @date ：Created in 2021/5/15  15:40
 * @description：TODO
 */
@RestController
@Api(description = "搜索微服务")
@RequestMapping("/api/list")
public class ListApiController {
//Spring官方 封装 ES  官方近期高级别的客户端
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//搜索性能极佳
//Spring官方 封装 ES官方早期低级别客户端
//    @Autowired
//    private ElasticsearchTemplate elasticsearchTemplate;
    //ES  官方近期高级别的客户端
    @Autowired
    private RestHighLevelClient restHighLevelClient;//搜索性能极佳
//    ES官方早期低级别客户端
//    @Autowired
//    private TransportClient transportClient;
    @Autowired
    private ListService listService;
    //创建索引 并 Mapping映射
    @GetMapping("/createIndex")
    public Result createIndex(){
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //创建mapping映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }
//这里的上架 下架  其实没啥用  是为了前期 swagger 测试 没有与后台连接  懒得删除了
    //上架之导入数据
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架之删除数据
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }
    //增加热度
    @GetMapping("/hotScore/{skuId}")
    public Result hostScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }
    //开始搜索   https://list.gmall.com/list.html
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }

}
